//
//  FCAddRelationViewController.m
//  customerRelation_Example
//
//  Created by 陈文斌 on 2019/2/16.
//  Copyright © 2019 64912907. All rights reserved.
//

#import "FCAddRelationViewController.h"
#import "TopNoticeLabel.h"
#import "FCAddRelationListViewController.h"
#import "FCBtnTypeRuleView.h"
#import "FCAlertViewController.h"
#import "CustomerRelationHeader.h"
#import "UIViewController+Permiss.h"

@interface FCAddRelationViewController ()
@property (strong, nonatomic) TopNoticeLabel *topNoticeLabel;
@property (nonatomic, strong) FCAddRelationListViewController *relationContentVC;

@end

@implementation FCAddRelationViewController

- (void)setupUI{
    self.title = customerRelationLocaChanged(@"添加上下游关系", @"添加上下游关系");
    self.view.backgroundColor = FCBackColor;
    
    TopNoticeLabel *topNoticeLabel = [[TopNoticeLabel alloc] init];
    topNoticeLabel.noticeTxt = customerRelationLocaChanged(@"手机号为企业创建者手机号；添加成功将发送关系绑定 邀请，待对方同意后，则上下游关系添加成功", @"手机号为企业创建者手机号；添加成功将发送关系绑定 邀请，待对方同意后，则上下游关系添加成功");
    topNoticeLabel.noticeLabel.font = font13;
    // 计算高度
    CGSize maximumLabelSize = CGSizeMake(WJCScreenW - (16 * 2 + 20 + 10), CGFLOAT_MAX);// 16:内边距;20:关闭按钮宽度;10:内边距
    CGSize expectSize = [topNoticeLabel.noticeLabel sizeThatFits:maximumLabelSize];
    topNoticeLabel.frame = CGRectMake(0, 0, WJCScreenW, expectSize.height + 20);
    [self.view addSubview:topNoticeLabel];
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topNoticeLabel.frame), WJCScreenW, WJCScreenH - expectSize.height)];
    [self.view addSubview:contentView];
    CGFloat rowHeight = IphonePlus ? 55 : 48;
    UIView *contentListView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WJCScreenW, rowHeight * 5)];
    [contentView addSubview:contentListView];
    
    topNoticeLabel.closeBlock = ^{
        XMGLog(@"关闭");
        [UIView animateWithDuration:0.25 animations:^{
            contentView.y = 0;
            contentView.height += expectSize.height;
        } completion:nil];
    };
    
    _relationContentVC = [[FCAddRelationListViewController alloc] init];
    [self addChildViewController:_relationContentVC];
    [_relationContentVC didMoveToParentViewController:self];
    [_relationContentVC.view setFrame:CGRectMake(0, 0, WJCScreenW, contentView.frame.size.height)];
    [contentListView addSubview:_relationContentVC.view];
    
    UIButton *inviteButtom = [UIButton buttonWithType:UIButtonTypeCustom];
    inviteButtom.frame = CGRectMake(38, CGRectGetMaxY(contentListView.frame) + 24, WJCScreenW - 38 * 2, 42);
    [inviteButtom setTitle:customerRelationLocaChanged(@"立即邀请", @"立即邀请") forState:UIControlStateNormal];
    [inviteButtom setBackgroundColor:HEXCOLOR(0xaccfff)];
    [inviteButtom setEnabled:NO];
    inviteButtom.layer.cornerRadius = 4;
    [inviteButtom addTarget:self action:@selector(inviteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:inviteButtom];
    
    _relationContentVC.buttonStateBlock = ^(BOOL isEnabled) {
        [inviteButtom setEnabled:isEnabled];
        if (isEnabled) {
            [inviteButtom setBackgroundColor:FCBlueBackColor];
        } else{
            [inviteButtom setBackgroundColor:HEXCOLOR(0xaccfff)];
        }
    };
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - 立即邀请
- (void)inviteButtonAction{
    [self.view endEditing:YES];
    WJCWeakSelf
    [self checkPermissionWithCode:@"Crm:Relations.View" andReturnBlock:^(FCPermissSubCellModel *permission) {
        if (permission.isAllowedByCurrIdentity) {
            [_relationContentVC showSheet];
        } else {
            weakSelf.permissionCode=@"Crm:Relations.View";
            [weakSelf checkPermissionandReturnBlock:^(FCPermissSubCellModel * _Nonnull permission) {
                
            }];
            return;
        }
    }];
    
    return ;
    
    if ([FCEmployeeModel isBusinessWithNotice:YES andPushController:self]) {
        [_relationContentVC showSheet];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

@end
